package com.sip.stream.utils.zfy.player;

import android.content.Context;
import android.media.MediaCodec;
import android.media.MediaCrypto;
import android.media.MediaFormat;
import android.util.Log;

import com.sip.stream.helper.GS28181Helper;
import com.sip.stream.media.ZFYMediaManager;
import com.sip.stream.utils.zfy.IVideoConsumer;

import java.io.IOException;

public class SipRemoteVideoPlayer extends AbstractRemoteVideoPlayer implements IVideoConsumer {
    public static final String TAG = "SipRemoteVideoPlayer";
    private Thread decodeThread;
    private MediaCodec mCodec;

    public interface SipRemoteVideoPlayerListener {
    }

    public SipRemoteVideoPlayer(Context context) {
        super(context);
    }

    @Override
    public void startPlay() {
        try {
            initDecoder();
            GS28181Helper.getInstance().setVideoConsumer(this);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private boolean initDecoder() throws IOException {
        MediaCodec createDecoderByType = MediaCodec.createDecoderByType("video/avc");
        Log.w(TAG, createDecoderByType.getName());
        MediaFormat createVideoFormat = MediaFormat.createVideoFormat("video/avc", ZFYMediaManager.getInstance().getConfig().getStreamWidth(), ZFYMediaManager.getInstance().getConfig().getStreamHeight());
        createVideoFormat.setString("mime", "video/avc");
        createDecoderByType.configure(createVideoFormat, getSurface(), (MediaCrypto) null, 0);
        createDecoderByType.start();
        Log.i(TAG, "mCodec configure");
        this.mCodec = createDecoderByType;
        Log.i(TAG, "mCodec start");
        Thread thread = new Thread() {
            private final MediaCodec.BufferInfo mediaCodecBufferInfo = new MediaCodec.BufferInfo();

            @Override
            public void run() {
                while (SipRemoteVideoPlayer.this.mCodec != null) {
                    try {
                        int dequeueOutputBuffer = SipRemoteVideoPlayer.this.mCodec.dequeueOutputBuffer(this.mediaCodecBufferInfo, -1L);
                        if (dequeueOutputBuffer >= 0) {
                            SipRemoteVideoPlayer.this.mCodec.releaseOutputBuffer(dequeueOutputBuffer, true);
                        } else if (dequeueOutputBuffer == -2) {
                            Log.i(SipRemoteVideoPlayer.TAG, "getOutputFormat " + SipRemoteVideoPlayer.this.mCodec.getOutputFormat());
                            int integer = SipRemoteVideoPlayer.this.mCodec.getOutputFormat().getInteger("width");
                            int integer2 = SipRemoteVideoPlayer.this.mCodec.getOutputFormat().getInteger("height");
                            Log.i(SipRemoteVideoPlayer.TAG, "KEY_WIDTH " + SipRemoteVideoPlayer.this.mCodec.getOutputFormat().getInteger("width") + " KEY_HEIGHT " + SipRemoteVideoPlayer.this.mCodec.getOutputFormat().getInteger("height"));
                            SipRemoteVideoPlayer.this.setVideoHolder(false);
                            SipRemoteVideoPlayer.this.onVideoConfigure(integer, integer2);
                        }
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                        Log.w(SipRemoteVideoPlayer.TAG, "" + e.getMessage());
                    }
                }
            }
        };
        this.decodeThread = thread;
        thread.start();
        return true;
    }

    @Override
    public void release() {
        GS28181Helper.getInstance().setVideoConsumer(null);
        try {
            Thread thread = this.decodeThread;
            if (thread != null) {
                thread.interrupt();
                this.decodeThread = null;
            }
        } catch (Exception unused) {
        }
        try {
            MediaCodec mediaCodec = this.mCodec;
            if (mediaCodec != null) {
                this.mCodec = null;
                mediaCodec.stop();
                mediaCodec.release();
            }
        } catch (Exception unused2) {
        }
    }

    @Override
    public void onVideoFrameIn(byte[] bArr, long j) {
        MediaCodec mediaCodec = this.mCodec;
        if (mediaCodec != null) {
            try {
                int dequeueInputBuffer = mediaCodec.dequeueInputBuffer(0L);
                if (dequeueInputBuffer >= 0) {
                    this.mCodec.getInputBuffer(dequeueInputBuffer).put(bArr);
                    this.mCodec.queueInputBuffer(dequeueInputBuffer, 0, bArr.length, System.nanoTime(), 0);
                }
            } catch (IllegalStateException e) {
                e.printStackTrace();
            }
        }
    }
}
